<!--关键词页面-->
<template>
  <!--  关键词视图-->
  <div
    ref="chart"
    style="margin: 0 auto; width: 600px; height:300px;"
    :data="data"
  />
</template>

<script>
import axios from 'axios'
import 'echarts-wordcloud/dist/echarts-wordcloud'
import 'echarts-wordcloud/dist/echarts-wordcloud.min'

export default {
  name: 'KeyWord',
  data() {
    return {
      // 主要数据
      data: [],
      // 配置数据
      option: {
        title: {
          text: 'KeyWords',
          x: 'center'
        },
        backgroundColor: '#fff',
        // tooltip: {
        //   pointFormat: "{series.name}: <b>{point.percentage:.1f}%</b>"
        // },
        series: [
          {
            type: 'wordCloud',
            // 用来调整词之间的距离
            gridSize: 10,
            // 用来调整字的大小范围
            // Text size range which the value in data will be mapped to.
            // Default to have minimum 12px and maximum 60px size.
            sizeRange: [14, 60],
            // Text rotation range and step in degree. Text will be rotated randomly in range [-90,                                                                             90] by rotationStep 45
            // 用来调整词的旋转方向，，[0,0]--代表着没有角度，也就是词为水平方向，需要设置角度参考注释内容
            // rotationRange: [-45, 0, 45, 90],
            // rotationRange: [ 0,90],
            rotationRange: [0, 0],
            // 随机生成字体颜色
            // maskImage: true,
            textStyle: {
              normal: {
                color: function() {
                  return (
                    'rgb(' +
                    Math.round(Math.random() * 255) +
                    ', ' +
                    Math.round(Math.random() * 255) +
                    ', ' +
                    Math.round(Math.random() * 255) +
                    ')'
                  )
                }
              }
            },
            // 位置相关设置
            // Following left/top/width/height/right/bottom are used for positioning the word cloud
            // Default to be put in the center and has 75% x 80% size.
            left: 'center',
            top: 'center',
            right: null,
            bottom: null,
            width: '600px',
            height: '300px',
            // 数据
            data: this.data
          }
        ]
      }
    }
  },
  watch: {
    // data从服务器获取后数据发生改变,此处监视可以重新绘制视图
    data: {
      deep: true,
      handler(element) {
        this.option.series[0].data = this.data
        this.chart.setOption(this.option)
      }
    }
  },
  mounted() {
    // 绘制图标
    this.initChart()
    // 从服务端获取数据
    this.getData()
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$refs.chart)
      console.log('lll')
      console.log(this.option)
    },
    getData() {
      axios.get('http://localhost:10086/smart-admin-api/keyWords/ana/EN2008a,10').then((res) => {
        // 旧key到新key的映射
        res.data.data.forEach((element) => {
          this.data.unshift(
            {
              'name': Object.values(element)[1],
              'value': Object.values(element)[2]
            }
          )
        })
      })
        .catch((res) => {
          console.log(res)
        })
    }
  }
}
</script>
